package com.dirk.happie.utils;

import java.util.ArrayList;
import java.util.List;

import org.acra.ACRA;
import org.acra.ReportingInteractionMode;
import org.acra.annotation.ReportsCrashes;

import android.app.Application;
import android.content.SharedPreferences;
import android.widget.Toast;

import com.dirk.happie.R;
import com.dirk.happie.beans.BasketItem;
import com.dirk.happie.beans.CompanyBean;
import com.dirk.happie.beans.SettingsBean;

@ReportsCrashes(formKey = "dHl5WnJCa2VXM0t6aEx1OHFRcUN5UUE6MQ", mode = ReportingInteractionMode.TOAST, resToastText = R.string.app_name)
public class App extends Application {

	private String language;

	public String getLanguage() {
		return language;
	}

	public void setLanguage(String language) {
		this.language = language;
	}

	private String defFavoriteId;

	public String getDefFavoriteId() {
		return defFavoriteId;
	}

	public void setDefFavoriteId(String defFavoriteId) {
		this.defFavoriteId = defFavoriteId;
	}

	@Override
	public void onCreate() {
		super.onCreate();
		ACRA.init(this);
		language = getResources().getConfiguration().locale.getLanguage();
		happie_prefs = getSharedPreferences(getString(R.string.pref_file_name), MODE_PRIVATE);
		loggedIn = happie_prefs.getBoolean(Keys.KEY_USER_LOGGED_IN, false);
		defFavoriteId = happie_prefs.getString(Keys.KEY_DEF_FAVORITE_ID, "");
		if (defFavoriteId.equals("")) {
			Toast.makeText(this, getString(R.string.def_favorite_not_exits), Toast.LENGTH_SHORT).show();
		} else {
			Toast.makeText(this, getString(R.string.def_favorite_exists), Toast.LENGTH_SHORT).show();
		}
		if (loggedIn) {
			userId = happie_prefs.getString(Keys.KEY_USER_ID, "");
		}

		companies = Common.jsonToCompanyArray(happie_prefs.getString("Companies", "[]"));
		favorites = Common.jsonToCompanyArray(happie_prefs.getString("Favorites", "[]"));
		position = happie_prefs.getInt("Position", 0);
		if (favorites != null && favorites.size() > 0) {
			companyId = favorites.get(position).getCompanyId();
			companyLogo = favorites.get(position).getCompanyLogo();
		}
		settings = Common.jsonToSettingsBean(happie_prefs.getString("Settings", "{}"));
		basketList = Common.jsonToBasketArray(happie_prefs.getString("BasketList", "[]"));
		// setLanguage("nl");
	}
	
	private boolean loggedIn = false;

	public boolean isLoggedIn() {
		return loggedIn;
	}

	public void setLoggedIn(boolean loggedIn) {
		this.loggedIn = loggedIn;
	}

	private String userId;

	public String getUserId() {
		return userId;
	}

	public void setUserId(String userId) {
		this.userId = userId;
	}

	private String companyId;

	private int position;

	public int getPosition() {
		return position;
	}

	public void setPosition(int position) {
		this.position = position;
	}

	public String getCompanyId() {
		return companyId;
	}

	public void setCompanyId(String companyId) {
		this.companyId = companyId;
	}

	private String companyLogo = "";

	public String getCompanyLogo() {
		return companyLogo;
	}

	public void setCompanyLogo(String companyLogo) {
		this.companyLogo = companyLogo;
	}

	private ArrayList<CompanyBean> favorites = new ArrayList<CompanyBean>();

	public ArrayList<CompanyBean> getFavorites() {
		return favorites;
	}

	public void setFavorites(ArrayList<CompanyBean> favorites) {
		this.favorites = favorites;
	}

	private SettingsBean settings = new SettingsBean();

	public SettingsBean getSettings() {
		return settings;
	}

	public void setSettings(SettingsBean settings) {
		this.settings = settings;
	}
	
	private List<CompanyBean> companies;
	
	public List<CompanyBean> getCompanies() {
		return this.companies;
	}

	public void setCompanies(List<CompanyBean> paramCompanyBean) {
		this.companies = paramCompanyBean;
	}

	private ArrayList<BasketItem> basketList = new ArrayList<BasketItem>();

	public ArrayList<BasketItem> getBasketList() {
		return basketList;
	}

	public void setBasketList(ArrayList<BasketItem> basketList) {
		this.basketList = basketList;
	}

	private SharedPreferences happie_prefs;

	public SharedPreferences getHappie_prefs() {
		return happie_prefs;
	}

	public void setHappie_prefs(SharedPreferences happie_prefs) {
		this.happie_prefs = happie_prefs;
	}

	public void addDefaultFavorite() {
		for (CompanyBean companyBean : this.companies) {
			if (this.defFavoriteId.equals(companyBean.getCompanyId())) {
				Common.addFavoriteIfExists(this.favorites, companyBean);
			}
		}
	}

	private boolean offerExists;

	public boolean isOfferExists() {
		return offerExists;
	}

	public void setOfferExists(boolean offerExists) {
		this.offerExists = offerExists;
	}
}
